package com.example.printservice.utils;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class PageIndexUtil {
    public static List<Integer> getPageIndex(String s) {
        List<Integer> pageIndex = new ArrayList<>();
        String pattern = ".*-.*";

        if (Pattern.matches(pattern, s)) {
            String[] indexes = s.split("-");
            try {
                int start = Integer.parseInt(indexes[0]);
                int end = Integer.parseInt(indexes[1]);
                if (start < 1 || end < start) throw new RuntimeException();
                for (int i = start; i <= end; i++) {
                    pageIndex.add(i);
                }
            } catch (NumberFormatException e) {
                throw new RuntimeException(e);
            }
        } else if (s.split("、").length > 0 && !StrUtil.isBlank(s)) {
            String[] in = s.split("、");
            for (String page : in) {
                try {
                    System.out.println(page);
                    int index = Integer.parseInt(page);
                    if (index < 1) throw new RuntimeException();
                    pageIndex.add(index);
                } catch (NumberFormatException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return pageIndex;
    }
}
